package com.soft.action.business;

import com.soft.model.*;
import com.soft.service.*;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * @program: garbgeSpringboot
 * @description: 商户订单管理
 **/
@Controller
@RequestMapping("business/orderManager")
public class OrderManager {

    @Autowired
    private GarbageOrderService garbageOrderService;

    @Autowired
    private AddressService addressService;

    @Autowired
    private GarbageTypeService garbageTypeService;

    @Autowired
    private SalesService salesService;


    @Autowired
    private UserService userService;

    // 跳转到垃圾回收订单管理界面
    @RequestMapping("/toOrderManager")
    public String toGarbageOrderManager(Map<String, Object> map) {

        List<GarbageOrder> garbageOrderList = garbageOrderService.getAllWaitGet();

        map.put("garbageOrderList", garbageOrderList);

        List<GarbageType> garbageTypeList = garbageTypeService.getAll();
        List<Address> addresses = addressService.getAll();

        map.put("garbageOrderList", garbageOrderList);
        map.put("addresses", addresses);
        map.put("garbageTypeList", garbageTypeList);

        return "business/garbageOrderManager";
    }

    // 跳转到垃圾回收订单管理界面
    @RequestMapping("/toAssigenSaler")
    public String toAssigenSaler(Map<String, Object> map, HttpSession session,@RequestParam("orderId")Integer orderId) {


        Business business = (Business) session.getAttribute("admin");
        Integer businessId = business.getId();
        List<Sales> salesList = salesService.getAll(businessId);

        map.put("salesList", salesList);
        map.put("orderId", orderId);

        return "business/saleList";
    }

    @RequestMapping(value = "/getGarbageOrder", method = RequestMethod.GET)
    public void getGarbageOrder(Map<String, Object> map, HttpSession session, HttpServletResponse response, @RequestParam("id") Integer id) throws IOException {

        PrintWriter out = response.getWriter();
        List<GarbageOrder> garbageOrderList = garbageOrderService.getAllWaitGet();


        Business business = (Business) session.getAttribute("admin");
        GarbageOrder garbageOrder = new GarbageOrder();

        garbageOrder.setId(id);
        garbageOrder.setStatus(2);
        garbageOrder.setBusinessId(business.getId());


        int result = garbageOrderService.updateSelective(garbageOrder);

        map.put("garbageOrderList", garbageOrderList);

        List<GarbageType> garbageTypeList = garbageTypeService.getAll();
        List<Address> addresses = addressService.getAll();

        map.put("garbageOrderList", garbageOrderList);
        map.put("addresses", addresses);
        map.put("garbageTypeList", garbageTypeList);

        // 添加垃圾上门回收订单
        if (result == 1) {
            System.out.println("抢单成功！");
            out.write("<script>alert('抢单成功！');"
                    + "window.location.href='/garbage-collection/business/orderManager/toOrderManager'</script>");
        } else {
            System.out.println("抢约失败！");
            out.write("<script>alert('抢单失败！');"
                    + "window.location.href='/garbage-collection/business/orderManager/toOrderManager'</script>");
        }
    }

    /**
     * 委派回收员
     */
    @RequestMapping(value = "/assigneSaler/{orderId}", method = RequestMethod.GET)
    public void assigneSaler(Map<String, Object> map, HttpSession session, HttpServletResponse response,
                             @PathVariable("orderId") Integer orderId,
                             @RequestParam("userId") Integer userId) throws IOException {

        PrintWriter out = response.getWriter();

        Business business = (Business) session.getAttribute("admin");
        GarbageOrder garbageOrder = new GarbageOrder();

        garbageOrder.setId(orderId);
        garbageOrder.setSaleId(userId);
        garbageOrder.setStatus(3);


        int result = garbageOrderService.updateSelective(garbageOrder);

        List<Sales> salesList = salesService.getAll(orderId);
        map.put("salesList", salesList);
        // 添加垃圾上门回收订单
        if (result == 1) {
            System.out.println("委派收货员成功！");
            out.write("<script>alert('委派收货员成功！');"
                    + "window.location.href='/garbage-collection/business/orderManager/toOrderManager'</script>");
        } else {
            System.out.println("委派收货员失败！");
            out.write("<script>alert('委派收货员失败！');"
                    + "window.location.href='/garbage-collection/business/orderManager/toOrderManager'</script>");
        }

    }

}